package com.secxiot.common.utils;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * 患者用户签名书
 */
public class SignWriteToPDF {

    public static void setWaterPrint(String outPDFPath, String inputPDFPath, String signImagePath) throws DocumentException, IOException {
        BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        InputStream input = new FileInputStream(new File(inputPDFPath));
        PdfReader reader = new PdfReader(input);

        OutputStream output = new FileOutputStream(new File(outPDFPath));
        PdfStamper stamper = new PdfStamper(reader, output);

        PdfContentByte page = stamper.getOverContent(1);
        //将文字贴入pdf
        BaseColor coler = new BaseColor(0, 0, 0);
        page.beginText();
        page.setFontAndSize(baseFont,12);
        page.setColorFill(coler);
        page.setTextMatrix(380,100); //设置文字在页面中的坐标
        page.showText(DateUtils.getYear() + " 年 " + DateUtils.getMonth() + " 月 " + DateUtils.getDay() + " 日 ");
        page.endText();

        PdfContentByte page2 = stamper.getOverContent(1);
        //将文字贴入pdf
        page2.beginText();
        page2.setFontAndSize(baseFont,12);
        page2.setColorFill(coler);
        page2.setTextMatrix(100,100); //设置文字在页面中的坐标
        page2.showText(DateUtils.getYear() + " 年 " + DateUtils.getMonth() + " 月 " + DateUtils.getDay() + " 日 ");
        page2.endText();

        //将图片贴入pdf
        Image image = Image.getInstance(signImagePath);
        image.setAbsolutePosition(480, 130); //设置图片在页面中的坐标
        //image.scalePercent(20);//依照比例缩放
        image.setRotationDegrees(90);//旋转 角度

        image.scaleAbsolute(image.getWidth()/10, image.getWidth()/10);
        page.addImage(image);

        stamper.close();
        reader.close();
        input.close();
    }

    public static void main(String[] args) throws DocumentException, IOException {
        setWaterPrint("H:\\photo\\spatient_sign_13.pdf", "H:\\photo\\spatient_sign.pdf", "H:\\photo\\711664989302785.png");
        pdfTOimg("H:\\photo\\spatient_sign_13.pdf","H:\\photo\\spatient_sign_13.png","png");
    }

    /**
     *
     * @param pdfPath pdf 路径
     * @param imgPath 图片路径
     * @param imgType 图片类型
     */
    public static void pdfTOimg(String pdfPath, String imgPath, String imgType) {
        File file = new File(pdfPath);
        try {
            PDDocument doc = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            for (int i = 0; i < pageCount; i++) {

                BufferedImage image = renderer.renderImageWithDPI(i, 300);
                //          BufferedImage image = renderer.renderImage(i, 2.5f);
                ImageIO.write(image, imgType, new File(imgPath));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除文件
     * @param fileName 文件路径
     * @return
     */
    public static boolean deleteFile(String fileName) {
        File file = new File(fileName);
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}